﻿using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

namespace IQIGame.Onigao.GamePlay
{
    [Serializable]
    public class UnitEffectClip : PlayableAsset, ITimelineClipAsset
    {
        [SerializeField] public string effectPath;
        [SerializeField] public string effectName;
        [SerializeField] public int dummy;
        [SerializeField] public Vector3 shifting;
        [SerializeField] public bool loop = true;
        // [SerializeField] public EVFXSpace followType;

        [SerializeField] public float defaultLength;

        public override double duration => 2.0;

        UnitEffectBehaviour behavior;
        public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
        {
            //var rPlayable = ScriptPlayable<UnitEffectBehaviour>.Create(graph);
            var rPlayable = UnitEffectBehaviour.Create(graph, owner);
            this.behavior = rPlayable.GetBehaviour();
            this.behavior.selfDummy = this.dummy;
            this.behavior.effectName = this.effectName;
            this.behavior.effectPath = this.effectPath;
            this.behavior.loop = this.loop;
            // this.behavior.followType = this.followType;
            this.behavior.defaultLength = this.defaultLength;
            this.behavior.shifting = this.shifting;

#if UNITY_EDITOR
            if (!Application.isPlaying)
                PrepareInstanceGo();
#endif
            return rPlayable;
        }

        public ClipCaps clipCaps
        {
            get => ClipCaps.None;
        }

#if UNITY_EDITOR
        public GameObject InstanceGo;
        public void SetPath(string p, GameObject go)
        {
            effectPath = p.Replace("Assets/", "");
            effectName = Path.GetFileNameWithoutExtension(effectPath);
            InstanceGo = go;
            PrepareInstanceGo();
            behavior?.SetPreviewGo(InstanceGo);
        }

        string Path2Name(string path) 
        {
            var index = path.LastIndexOf('/');
            if (index < 0) return path;
            var name = path.Substring(index + 1, path.Length - index - 1);
            return name.Replace(".prefab", "");
        }

        //在场景中查找已实例化的prefab，如果不存在已实例化的prefab就自己实例化一个出来
        public void PrepareInstanceGo()
        {
            if (string.IsNullOrEmpty(effectPath)) return;
            var prefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/" + effectPath);
            if (prefab == null) return;

            InstanceGo = FindFirstPrefabInstancesInScene(prefab);
            if (InstanceGo == null)
                InstanceGo = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
            behavior?.SetPreviewGo(InstanceGo);
        }

        GameObject FindFirstPrefabInstancesInScene(GameObject prefab)
        {
            // 获取场景中所有 GameObject
            GameObject[] allObjects = FindObjectsOfType<GameObject>(true);

            for (int i = 0; i < allObjects.Length; i++)
            {
                var sourcePrefab = PrefabUtility.GetCorrespondingObjectFromSource(allObjects[i]);
                if (sourcePrefab == prefab)
                    return allObjects[i];
            }
            return null;
        }

#endif
    }
}